import Vue from 'vue';
var _ = require("lodash")

const state = {
  filter:{
      rows: 10,
      page: 1,
      total: 0,
  },
  cartGoods:[],
};

const getters = {
  getFilterGoods:state => {
    return state.filter;
  },
  getCartGoods:state => {
    return state.cartGoods;
  }
};

const mutations = {
  setFilterGoods:(state,payload) => {
    state.filter = payload;
  },
  setCartGoods:(state,payload) => {
    var pos = _.findIndex(state.cartGoods,{'_id':payload._id});//匹配
    if(pos == -1){
      payload.number = 1;
      state.cartGoods.push(payload);
    }else{
      state.cartGoods[pos].number += 1;
    }

  },
}

const actions = {
  setFilterGoods:({commit},payload)=>{
    commit("setFilterGoods",payload)
  },
  setCartGoods:({commit},payload)=>{
    commit("setCartGoods",payload)
  },
}

export default {
  state,
  getters,
  mutations,
  actions
}
